<?php
namespace app\index\controller;

//大学生活动态类
class Life extends Base
{
    /**
     * 用户发布校园动态
     */
    public function publishLife()
    {
        $data = input('post.');

        //校验数据
        $validate = \think\Loader::validate('Life');
        if(!$validate->check($data)){
            $info = array('code' => '400','message'=>$validate->getError());
            echo displayJson($info);die;
        }

        $data['sl_create_time'] = time();

        $res = db('school_life',[],false)->insert($data);

        if ($res) {
            $info['code']     = '200';
            $info['message']  = '发布成功';
            echo displayJson($info);die;
        } else {
            $info['code']     = '400';
            $info['message']  = '发布失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 查看我发布的校园生活
     */
    public function getMySchoolLife()
    {
        $uid = input('uid');

        if (empty($uid)) {
            $info['code']     = '401';
            $info['message']  = '数据异常，系统自动退出';
            echo displayJson($info);die;
        }

        $list = db('school_life',[],false)->where('sl_uid',$uid)->where('sl_status',1)->order('sl_create_time desc')->select();

        if ($list) {
            foreach ($list as $key => $val) {
                $con =  strip_tags($val['sl_content']);
                if(mb_strlen($con) >100){
                    $list[$key]['sl_content'] = mb_substr($con,0,100,'UTF-8') .'...';
                }

                $list[$key]['sl_create_time'] = date('Y-m-d',$val['sl_create_time']);
            }
        }

        $info['code']     = '200';
        $info['data']     = $list;
        echo displayJson($info);die;
    }

    /**
     * 编辑我发布的校园生活
     */
    public function editMySchoolLife()
    {
        $data = input('post.');

        if (empty($data['sl_uid']) || empty($data['sl_id'])) {
            $info['code']     = '401';
            $info['message']  = '数据异常，系统自动退出';
            echo displayJson($info);die;
        }

        //校验数据
        $validate = \think\Loader::validate('Life');
        if(!$validate->check($data)){
            $info = array('code' => '400','message'=>$validate->getError());
            echo displayJson($info);die;
        }
        $sid = $data['sl_id'];
        unset($data['sl_id']);

        $res = db('school_life',[],false)->where('sl_id',$sid)->update($data);

        if ($res) {
            $info['code']     = '200';
            $info['message']  = '编辑成功';
            echo displayJson($info);die;
        } else {
            $info['code']     = '400';
            $info['message']  = '编辑失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 删除我发布的校园生活
     */
    public function delMySchoolLife()
    {
        $sl_id = input('sl_id');

        if (empty($sl_id)) {
            $info['code']     = '401';
            $info['message']  = '数据异常，系统自动退出';
            echo displayJson($info);die;
        }

        $set['sl_status'] = 2;
        $del = db('school_life',[],false)->where('sl_id',$sl_id)->update($set);

        if ($del) {
            $info['code']     = '200';
            $info['message']  = '删除成功';
            echo displayJson($info);die;
        } else {
            $info['code']     = '400';
            $info['message']  = '删除失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 获取校园生活详情--在登录的情况下查看
     */
    public function getLifeDesc()
    {
        $sl_id = input('sl_id');
        $uid   = input('uid'); //用户id

        if (empty($sl_id) || empty($uid)) {
            $info['code']     = '401';
            $info['message']  = '数据异常，系统自动退出';
            echo displayJson($info);die;
        }

        $get = db('school_life',[],false)->where('sl_id',$sl_id)->find();

        if ($get) {
            $info['code']     = '200';
            $info['data']     = $get;
            echo displayJson($info);die;
        } else {
            $info['code']     = '400';
            $info['message']  = '数据获取失败，请刷新重试';
            echo displayJson($info);die;
        }
    }

}